package com.film.adminFilter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.Admin;

@WebFilter("/admin_jsp/*")
public class AdminFilter extends HttpFilter{

	@Override
	protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
//		//创建一个数组来记录需要拦截的资源
//		String[] uriArray = {"/adminHotFilm.do","/showFilmExhibition.do",
//				"/filmType.do","/film/ReviewTypePage.do?page=1",
//				"/film/director.do","/film/adminPerformer.do",
//				"/film/userShow.do?page=1","director.jsp",
//				"filmTypeExhibition.jsp","hotFilmExhibition.jsp",
//				"hotOffTheShelfFilm.jsp","main.jsp","performer.jsp",
//				"review.jsp","showFilmExhibition.jsp",
//				"showOffTheShelfFilm.jsp","userShow.jsp",
//				"welcome.jsp","/addShowings.do","/filmShowings.do",
//				"/filmShowingsPage.do","filmShowings.jsp"
//		};
//		//获取当前请求的uri
//		String uri = request.getRequestURI();
//		boolean flag = false;
//		for (int i = 0; i < uriArray.length; i++) {
//			if(uri.contains(uriArray[i])) {
//				flag = true;
//				break;
//			}
//		}
//		if(flag) {
//			HttpSession session = request.getSession();
//			Admin admin=(Admin)session.getAttribute("admin");
//			if(admin != null) {
//				chain.doFilter(request, response);
//				return;
//			}else {
//				response.sendRedirect("/film/admin_jsp/index.jsp");
//				return;
//			}
//		}else {
//			chain.doFilter(request, response);
//			return;
//		}
		HttpSession session = request.getSession();
		Admin admin = (Admin)session.getAttribute("admin");
		if(admin != null || request.getRequestURI().contains("/admin_jsp/index.jsp")) {
			chain.doFilter(request, response);
		}else {
			response.sendRedirect("/film/admin_jsp/index.jsp");
		}
	}
}
